﻿
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WLMes.Models;
using WLMes;
using System.Diagnostics;

namespace WLMes
{
    public class MesMutua
    {
        public MesMutua(string url)
        {
            this.Url = url;
        }
        public string Url { get; set; }

        public bool IsConnected
        {
            get
            {
                string ipAddress = Url.Split(':')[1].Replace("//", "");
                try
                {
                    Ping objPingSender = new Ping();
                    PingOptions objPinOptions = new PingOptions();
                    objPinOptions.DontFragment = true;
                    string data = "";
                    byte[] buffer = Encoding.UTF8.GetBytes(data);
                    int intTimeout = 120;
                    PingReply objPinReply = objPingSender.Send(ipAddress, intTimeout, buffer, objPinOptions);
                    string strInfo = objPinReply.Status.ToString();
                    if (strInfo == "Success")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 上位机提供给MES调用的批次下发接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<ISSUE_TASK_response> ProductionData1(ISSUE_TASK_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 1, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<ISSUE_TASK_response>(stget);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// MES提供给产线上位机调用的批次工艺请求接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<ASK_TASK_response> ProductionData2(ASK_TASK_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 2, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<ASK_TASK_response>(stget);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        ///MES提供给产线上位机调用的批次大板来料交互请求接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>

        public async Task<LABEL_ASK_response> ProductionData3(LABEL_ASK_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 3, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<LABEL_ASK_response>(stget);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// MES提供给产线上位机调用的批次大板来料交互请求接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<BOARD_ENTRY_response> ProductionData4(BOARD_ENTRY_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 4, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<BOARD_ENTRY_response>(stget);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// MES提供给产线上位机调用的过程测量值上传接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<MEASUREMENT_response> ProductionData5(MEASUREMENT_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 5, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<MEASUREMENT_response>(stget);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// MES提供给产线上位机调用的产品与托盘绑定信息的接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>

        public async Task<BIND_PALLET_response> ProductionData6(BIND_PALLET_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 6, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<BIND_PALLET_response>(stget);
                }
                catch
                {
                    BIND_PALLET_response enmp = new BIND_PALLET_response();
                    enmp.EventCode = 1;
                    enmp.EventDescription = "无法读取数据";
                    return enmp;
                }
            }
            else
            {
                BIND_PALLET_response enmp = new BIND_PALLET_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "BIND_PALLET网络不可达";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的批次完工信息接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<END_TASK_response> ProductionData7(END_TASK_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 7, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<END_TASK_response>(stget);
                }
                catch
                {
                    END_TASK_response enmp = new END_TASK_response();
                    enmp.EventCode = 1;
                    enmp.EventDescription = "无法读取数据";
                    return enmp;
                }
            }
            else
            {
                END_TASK_response enmp = new END_TASK_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "END_TASK网络不可达";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的缺料信息接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<LACK_MAT_response> ProductionData8(LACK_MAT_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 8, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<LACK_MAT_response>(stget);
                }
                catch
                {
                    LACK_MAT_response enmp = new LACK_MAT_response();
                    enmp.EventCode = 1;
                    enmp.EventDescription = "无法读取数据";
                    return enmp;
                }
            }
            else
            {
                LACK_MAT_response enmp = new LACK_MAT_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "LACK_MAT网络不可达";
                return enmp;
            }
        }
        /// <summary>
        /// MES提供给产线上位机调用的剩余物料信息接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<REMAINDER_MAT_response> ProductionData9(REMAINDER_MAT_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 9, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<REMAINDER_MAT_response>(stget);
                }
                catch
                {
                    REMAINDER_MAT_response enmp = new REMAINDER_MAT_response();
                    enmp.EventCode = 1;
                    enmp.EventDescription = "无法读取数据";
                    return enmp;
                }
            }
            else
            {
                REMAINDER_MAT_response enmp = new REMAINDER_MAT_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "REMAINDER_MAT网络不可达";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的不良品反馈信息接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<NG_REPORT_response> ProductionData10(NG_REPORT requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 10, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<NG_REPORT_response>(stget);
                }
                catch
                {
                    NG_REPORT_response enmp = new NG_REPORT_response();
                    enmp.EventCode = 1;
                    enmp.EventDescription = "无法读取数据";
                    return enmp;
                }
            }
            else
            {
                NG_REPORT_response enmp = new NG_REPORT_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "NG_REPORT网络不可达";
                return enmp;
            }
        }
        /// <summary>
        /// MES提供给产线上位机调用的托盘上线数据请求接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<PALLET_ENTRY_response> ProductionData11(PALLET_ENTRY_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 11, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<PALLET_ENTRY_response>(stget);
                }
                catch
                {
                    PALLET_ENTRY_response enmp = new PALLET_ENTRY_response();
                    enmp.EventCode = 1;
                    enmp.EventDescription = "无法读取数据";
                    return enmp;
                }
            }
            else
            {
                PALLET_ENTRY_response enmp = new PALLET_ENTRY_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "PALLET_ENTRY网络不可达";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的部件扫描上线数据请求接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>

        public async Task<PART_ENTRY_response> ProductionData12(PART_ENTRY_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 12, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<PART_ENTRY_response>(stget);
                }
                catch
                {
                    PART_ENTRY_response enmp = new PART_ENTRY_response();
                    enmp.EventCode = 1;
                    enmp.EventDescription = "无法读取数据";
                    return enmp;
                }
            }
            else
            {
                PART_ENTRY_response enmp = new PART_ENTRY_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "PART_ENTRY网络不可达";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的部件扫描下线数据请求接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<PART_EXIT_response> ProductionData13(PART_EXIT_request requestData)
        {
            if (IsConnected)
            {
                try
                {
                    WsHelper ws = new WsHelper();
                    string json = await ws.SendToReturn(Url, 13, JsonRef.ObjectToJson(requestData), "ProductionData");
                    string stget = ws.getjson(json, "ProductionDataResult");
                    return JsonConvert.DeserializeObject<PART_EXIT_response>(stget);
                }
                catch
                {
                    PART_EXIT_response enmp = new PART_EXIT_response();
                    enmp.EventCode = 1;
                    enmp.EventDescription = "无法读取数据";
                    return enmp;
                }
            }
            else
            {
                PART_EXIT_response enmp = new PART_EXIT_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "PART_EXIT网络不可达";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的能源数值上传接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<ENERGY_DATA_response> ProductionData14(ENERGY_DATA_request requestData)
        {

            try
            {
                WsHelper ws = new WsHelper();
                string json = await ws.SendToReturn(Url, 14, JsonRef.ObjectToJson(requestData), "EquipmentData");
                string stget = ws.getjson(json, "EquipmentDataResult");
                return JsonConvert.DeserializeObject<ENERGY_DATA_response>(stget);
            }
            catch
            {
                ENERGY_DATA_response enmp = new ENERGY_DATA_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "无法读取数据";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的运行模式接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<RUN_MODE_response> ProductionData15(RUN_MODE_request requestData)
        {
            try
            {
                WsHelper ws = new WsHelper();
                string json = await ws.SendToReturn(Url, 15, JsonRef.ObjectToJson(requestData), "EquipmentData");
                string stget = ws.getjson(json, "EquipmentDataResult");
                return JsonConvert.DeserializeObject<RUN_MODE_response>(stget);
            }
            catch
            {
                RUN_MODE_response enmp = new RUN_MODE_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "无法读取数据";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的运行模式接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<EQUIP_STATUS_response> ProductionData16(EQUIP_STATUS_request requestData)
        {
            try
            {
                WsHelper ws = new WsHelper();
                string json = await ws.SendToReturn(Url, 16, JsonRef.ObjectToJson(requestData), "EquipmentData");
                string stget = ws.getjson(json, "EquipmentDataResult");
                return JsonConvert.DeserializeObject<EQUIP_STATUS_response>(stget);
            }
            catch
            {
                EQUIP_STATUS_response enmp = new EQUIP_STATUS_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "无法读取数据";
                return enmp;
            }
        }

        /// <summary>
        /// 心跳接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<LIFE_BEAT_response> ProductionData17(LIFE_BEAT_request requestData)
        {

            try
            {
                WsHelper ws = new WsHelper();
                string json = await ws.SendToReturn(Url, 17, JsonRef.ObjectToJson(requestData), "EquipmentData");
                string stget = ws.getjson(json, "EquipmentDataResult");
                return JsonConvert.DeserializeObject<LIFE_BEAT_response>(stget);
            }
            catch
            {
                LIFE_BEAT_response enmp = new LIFE_BEAT_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "无法读取数据";
                return enmp;
            }
        }

        /// <summary>
        /// MES提供给产线上位机调用的运行模式接口
        /// </summary>
        /// <param name="requestCommand"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public async Task<EQUIP_Times_response> EquipmentData18(EQUIP_Times_request requestData)
        {
            try
            {
                WsHelper ws = new WsHelper();
                string json = await ws.SendToReturn(Url, 18, JsonRef.ObjectToJson(requestData), "EquipmentData");
                string stget = ws.getjson(json, "EquipmentDataResult");
                return JsonConvert.DeserializeObject<EQUIP_Times_response>(stget);
            }
            catch
            {
                EQUIP_Times_response enmp = new EQUIP_Times_response();
                enmp.EventCode = 1;
                enmp.EventDescription = "无法读取数据";
                return enmp;
            }
        }

        /// <summary>
        /// 遍历结构体,生成表t-sql创建语句
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="model">实例</param>
        /// <param name="ida">ID名称</param>
        /// <returns></returns>
        public string ModelProperties<T>(T model, string ida)
        {
            string sq = "";
            Type t = model.GetType();
            foreach (PropertyInfo info in t.GetProperties())
            {
                string name = info.Name;
                string type = info.PropertyType.Name;
                sq += $"{Refrence(info)},";
            }
            return $"create table {t.Name.ToLower()}(" + sq + ida + $" varchar(36),{t.Name.ToLower()}UserTime DateTime DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')))";
        }
        /// <summary>
        /// 遍历结构体,获取列名
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="model">实例</param>
        /// <param name="ida">ID名称</param>
        /// <returns></returns>
        public List<string> ModelColumns<T>(T model)
        {
            List<string> lis = new List<string>();
            Type t = model.GetType();
            foreach (PropertyInfo info in t.GetProperties())
            {
                string name = info.Name;
                string type = info.PropertyType.Name;
                lis.Add(Refrence(info));
            }
            return lis;
        }
        /// <summary>
        /// 匹配SQL类型
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        string Refrence(PropertyInfo info)
        {
            string type = info.PropertyType.Name;
            string name = info.Name;
            string str = "";
            switch (type)
            {
                case "UInt16":
                    str = name + " int";
                    break;
                case "String":
                    str = name + " varchar(50)";
                    break;
                case "Int32":
                    str = name + " int";
                    break;
                case "Double":
                    str = name + " real";
                    break;
                case "Boolean":
                    str = name + " boolean";
                    break;
                case "List`1":
                    str = name + "_ID varchar(20)";
                    break;
            }
            return str;
        }
    }
}
